<template>
    <div v-loading.fullscreen.lock="fullscreenLoading" element-loading-text="正在删除...">
        <el-button link type="danger" size="small" @click="deleteClick()">删除</el-button>
    </div>
</template>

<script>
import { ref } from '@vue/reactivity'
import { watch } from '@vue/runtime-core'
import { ElMessageBox,ElMessage, ElNotification } from 'element-plus'
import axios from 'axios';
import { useStore } from 'vuex'
export default {
    props:['deleteSubject'],
    setup(props){
        const store = useStore()
        //接收父组件传的参数
        let deleteSubject = ref()
        deleteSubject.value = props.deleteSubject
        watch(()=>props.deleteSubject,(newVal)=>{
            deleteSubject.value = newVal
        },{
            deep: true,
            immediate: false
        })

        //点击删除
        function deleteClick() {
            ElMessageBox.confirm(
                '确认删除?',
                '确认',
                {
                    confirmButtonText: '确认',
                    cancelButtonText: '取消',
                    type: 'warning',
                }
            ).then(() => {
                    
                    fullscreenLoading.value = true
                    axios.delete("/api/subject/deleteOneSubject", {
                        params: {
                            subjectId: deleteSubject.value.subjectId,
                            subjectTypeId: store.state.questionBank.subjectModel.subjectPagination.subjectTypeId,
                            accountTypeId: store.state.questionBank.subjectModel.subjectPagination.accountTypeId,
                            status: store.state.questionBank.subjectModel.subjectPagination.status,
                            pageNumber: store.state.questionBank.subjectModel.subjectPagination.pageNum,
                            pageSize: store.state.questionBank.subjectModel.subjectPagination.pageSize
                        }
                    }).then((response) => {
                        if (response.status == 200) {
                            if (response.data.code == 200) {
                                store.commit("selectSubjectTableData", response.data.data.list);
                                store.commit("selectSubjectTableTotal", response.data.data.total);
                                ElNotification({
                                    title: '操作成功',
                                    message: '删除成功',
                                    type: 'success',
                                })
                            } else {
                                ElNotification({
                                    title: '操作失败',
                                    message: '删除失败',
                                    type: 'error',
                                })
                            }
                        } else {
                            ElNotification({
                                title: '操作异常',
                                message: '删除异常',
                                type: 'warring',
                            })
                        }
                    }).finally(() => {
                        fullscreenLoading.value = false
                    })

                }).catch(() => {
                    ElMessage('操作取消')
                    fullscreenLoading.value = false
                })
        }
        //加载
        let fullscreenLoading = ref(false)
        return {
            deleteClick,
            fullscreenLoading
        }
    }
}
</script>

<style>

</style>